package com.ttxz.sample.model

import com.ttxz.common.base.model.BaseModel
import com.ttxz.common.http.HttpResponse
import com.ttxz.common.http.RetrofitManager
import com.ttxz.sample.MyApplication
import com.ttxz.sample.bean.ArticleListBean
import com.ttxz.sample.bean.ArticleListItemBean
import com.ttxz.sample.bean.BannerBean
import com.ttxz.sample.db.entity.ArticleListEntity
import com.ttxz.sample.db.entity.BannerEntity
import com.ttxz.sample.http.DemoApiServer

class MainModel : BaseModel() {

    suspend fun getBannerData(): HttpResponse<List<BannerBean>>{
        return executeHttp {
            RetrofitManager.create(DemoApiServer::class.java).getBanner()
        }
    }

    /**
     * 从数据库中查询数据
     */
    suspend fun getBannerDataFoDB(): List<BannerBean>{
        var bannerListDB = MyApplication.dbManager?.bannerDao()?.queryAll()
        if(bannerListDB.isNullOrEmpty()){
            bannerListDB = mutableListOf()
        }
        val resultList = mutableListOf<BannerBean>()
        var bannerBean:BannerBean? = null
        bannerListDB.forEach {
            bannerBean = BannerBean(it.id, it.imagePath?:"",it.isVisible,it.order,it.title?:"",it.type,it.url?:"",it.desc?:"")
            resultList.add(bannerBean!!)
        }

        return resultList
    }

    suspend fun getArticleListData(): HttpResponse<ArticleListBean>{
        return executeHttp {
            RetrofitManager.create(DemoApiServer::class.java).getArticleList(0)
        }
    }
    suspend fun getArticleListDataForDB(): List<ArticleListItemBean>{
        var articleListItemList = MyApplication?.dbManager?.articleDao()?.queryAll()
        if (articleListItemList.isNullOrEmpty()) {
            articleListItemList = mutableListOf()
        }
        val resultList = mutableListOf<ArticleListItemBean>()
        var itemBean:ArticleListItemBean? = null
        articleListItemList.forEach {
            itemBean = ArticleListItemBean(it.author?:"",it.shareUser?:"",it.superChapterName?:"",it.title?:"",it.niceShareDate?:"",it.link?:"")
            resultList.add(itemBean!!)
        }
        return resultList
    }

    suspend fun insertBannerDataForDb(bannerList:List<BannerBean>){

        val insertList = mutableListOf<BannerEntity>()
        var bannerEntity:BannerEntity? = null
        bannerList.forEach {
            bannerEntity = BannerEntity(it.id,it.imagePath,it.isVisible,it.order,it.title,it.type,it.url,it.desc)
            insertList.add(bannerEntity!!)
        }
        MyApplication.dbManager?.bannerDao()?.deleteAll()
        MyApplication.dbManager?.bannerDao()?.insertList(insertList)
    }

    suspend fun insertArticleListDataForDb(articleListItemBean: List<ArticleListItemBean>) {
        val insertList = mutableListOf<ArticleListEntity>()
        var articleListEntity:ArticleListEntity? = null
        articleListItemBean.forEach {
            articleListEntity = ArticleListEntity(null,it.author,it.shareUser,it.superChapterName,it.title,it.niceShareDate,it.link)
            insertList.add(articleListEntity!!)
        }
        MyApplication.dbManager?.articleDao()?.deleteAll()
        MyApplication.dbManager?.articleDao()?.insertList(insertList)
    }

}